package com.ehotting.mqbd.zw.client.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ehotting.eaf.core.utils.AssertUtils;
import com.ehotting.eaf.core.utils.StringUtils;
import com.ehotting.eaf.netty.constant.NettyState;
import com.ehotting.eaf.netty.domain.DataAble;
import com.ehotting.eaf.netty.domain.Response;
import com.ehotting.mqbd.api.bean.MQDataAble;
import com.ehotting.mqbd.api.bean.MqApplicationContext;
import com.ehotting.mqbd.api.enums.DirectionEnum;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.core.logger.MqMsgLogger;
import com.ehotting.mqbd.core.mq.consumer.action.ConsumerResponseAction;
import com.ehotting.mqbd.core.manager.AgentLinkManager;
import com.ehotting.mqbd.core.manager.ModuleManager;
import com.ehotting.mqbd.core.sync.MqSyncFutureMapHelper;
import com.ehotting.mqbd.zw.api.bean.ZwMQTcpData;
import com.ehotting.mqbd.zw.api.constants.ActionConstants;
import com.ehotting.mqbd.zw.api.domain.netty.ZwCommPacket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MqTcpConsumerResponseAction extends ConsumerResponseAction {

    private static final Logger logger = LoggerFactory.getLogger(MqTcpConsumerResponseAction.class);

    protected ZwMqActionFactory zwMqActionFactory;

    protected AgentLinkManager agentLinkManager;


    private ZwMqActionServiceAble zwMqActionService;

    public MqTcpConsumerResponseAction(MqSyncFutureMapHelper<MQDataAble> mqSyncFutureMapHelper,
                                       MqApplicationContext mqApplicationContext,
                                       TopicEnum topic,
                                       DirectionEnum direction,
                                       ModuleManager moduleManager,MqMsgLogger mqMsgLogger) {
        super(mqSyncFutureMapHelper, mqApplicationContext, topic, direction, moduleManager,mqMsgLogger);
    }

    public MqTcpConsumerResponseAction(MqSyncFutureMapHelper<MQDataAble> mqSyncFutureMapHelper,
                                       MqApplicationContext mqApplicationContext,
                                       TopicEnum topic,
                                       DirectionEnum direction,
                                       String nodeIp,
                                       ModuleManager moduleManager, MqMsgLogger mqMsgLogger,AgentLinkManager agentLinkManager) {
        super(mqSyncFutureMapHelper, mqApplicationContext, topic, direction, nodeIp, moduleManager,mqMsgLogger);
        this.agentLinkManager = agentLinkManager;
    }

    @Override
    public void extInvoke(MQDataAble data) {
        if(!(data instanceof ZwMQTcpData)){
            logger.error("!(data instanceof ZwMQTcpData)[serial:"+data.getSerial()+"]");
            zwMqActionService.sendToClient(data);
            return;
        }
        ZwCommPacket response = ((ZwMQTcpData)  data).getResponse();
        String msg = JSON.toJSONString(response);
        if(response == null){
            logger.error("response is null,return,data:{}",JSON.toJSONString(data));
            return;
        }
        DataAble result = null;
        String serial = null;
        try {
            String action = response.getAction();
            if (StringUtils.isBlank(action)) {
                logger.error("action is null,data is {},return.", msg);
                return;
            }

            ZwMqActionAble<ZwCommPacket> nettyAction = zwMqActionFactory.getAction(action);
            if (nettyAction == null) {
                logger.error("nettyAction[action={}] is null,return.",  action);
                return;
            }

            ZwCommPacket req = (ZwCommPacket) JSON.parseObject(JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect),nettyAction.getRequestClass());

            result = nettyAction.invoke(req);
        } catch (Exception e) {
            logger.error("data:{}", msg);
            logger.error("nettyAction invoke error", e);
            result = new Response(NettyState.ACTION_EXCETION, NettyState.FAIL, serial, e.getMessage());
        } finally {
            /**返回数据**/
            if (result != null) {
                String resultString = JSON.toJSONString(result, SerializerFeature.DisableCircularReferenceDetect);
                logger.info("result:{}", resultString);
            }
        }
    }

    public void setZwMqActionFactory(ZwMqActionFactory zwMqActionFactory) {
        this.zwMqActionFactory = zwMqActionFactory;
    }

    public void setZwMqActionService(ZwMqActionServiceAble zwMqActionService) {
        this.zwMqActionService = zwMqActionService;
    }
}
